/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Global Trust Authority is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

use attestation::error::attestation_error::AttestationError;
use actix_web::http::StatusCode;
use std::io;

#[cfg(test)]
mod attestation_error_tests {
    use super::*;

    #[test]
    fn test_invalid_parameter_error() {
        let error = AttestationError::InvalidParameter("test parameter".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Invalid parameter: test parameter");
        assert_eq!(format!("{}", error), "Invalid parameter: test parameter");
    }

    #[test]
    fn test_create_nonce_verification_error_should_return_bad_request_status() {
        let error = AttestationError::NonceVerificationError("invalid nonce".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Nonce verification failed: invalid nonce");
        assert_eq!(format!("{}", error), "Nonce verification failed: invalid nonce");
    }

    #[test]
    fn test_policy_verification_error() {
        let error = AttestationError::PolicyVerificationError("policy check failed".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Policy verification failed: policy check failed");
        assert_eq!(format!("{}", error), "Policy verification failed: policy check failed");
    }

    #[test]
    fn test_evidence_verification_error() {
        let error = AttestationError::EvidenceVerificationError("invalid evidence".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Evidence verification failed: invalid evidence");
        assert_eq!(format!("{}", error), "Evidence verification failed: invalid evidence");
    }

    #[test]
    fn test_token_generation_error() {
        let error = AttestationError::TokenGenerationError;
        assert_eq!(error.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_database_error() {
        let error = AttestationError::DatabaseError;
        assert_eq!(error.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_internal_error() {
        let error = AttestationError::InternalError("unexpected error".to_string());
        assert_eq!(error.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(error.message(), "Internal Server Error: unexpected error");
        assert_eq!(format!("{}", error), "Internal Server Error: unexpected error");
    }

    #[test]
    fn test_plugin_not_found_error() {
        let error = AttestationError::PluginNotFoundError("tpm_boot plugin".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Plugin not found: tpm_boot plugin");
        assert_eq!(format!("{}", error), "Plugin not found: tpm_boot plugin");
    }

    #[test]
    fn test_policy_not_found_error() {
        let error = AttestationError::PolicyNotFoundError("default policy".to_string());
        assert_eq!(error.status_code(), StatusCode::BAD_REQUEST);
        assert_eq!(error.message(), "Policy not found: default policy");
        assert_eq!(format!("{}", error), "Policy not found: default policy");
    }

    #[test]
    fn test_from_io_error() {
        let io_error = io::Error::new(io::ErrorKind::NotFound, "file not found");
        let attestation_error: AttestationError = io_error.into();
        
        match attestation_error {
            AttestationError::InternalError(msg) => {
                assert!(msg.contains("file not found"));
            }
            _ => panic!("Expected InternalError variant"),
        }
    }

    #[test]
    fn test_from_serde_json_error() {
        let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let attestation_error: AttestationError = json_error.into();
        
        match attestation_error {
            AttestationError::InternalError(msg) => {
                assert!(msg.contains("expected"));
            }
            _ => panic!("Expected InternalError variant"),
        }
    }

    #[test]
    fn test_from_string() {
        let error_msg = "test error message".to_string();
        let attestation_error: AttestationError = error_msg.into();
        
        match attestation_error {
            AttestationError::InternalError(msg) => {
                assert_eq!(msg, "test error message");
            }
            _ => panic!("Expected InternalError variant"),
        }
    }

    #[test]
    fn test_from_str() {
        let error_msg = "test error message";
        let attestation_error: AttestationError = error_msg.into();
        
        match attestation_error {
            AttestationError::InternalError(msg) => {
                assert_eq!(msg, "test error message");
            }
            _ => panic!("Expected InternalError variant"),
        }
    }

    #[test]
    fn test_error_debug_format() {
        let error = AttestationError::InvalidParameter("test".to_string());
        let debug_str = format!("{:?}", error);
        assert!(debug_str.contains("InvalidParameter"));
        assert!(debug_str.contains("test"));
    }
}